import { getJSON } from '@quickearth/core'
import dayjs from 'dayjs'
import apiList from '../api/api'

const defaultData = () => ({
  timerIndex: 0,
  moduleVisible: {
    //实况
    skModule: true,
    //预报
    ybModule: false
  },
  componentsVisible: {
    //网格
    GDZ: false,
    //色斑图
    SBT: true,
    // 站点名称
    zdmc: false,
    // 站号
    zh: false,
    // 县名
    xm: false,
    labels: '全部'
  },
  stationInfo: [],
  // 站点实况监测 当前站点信息
  // 分钟数据
  zdskjcDataList: [],
  // 小时数据
  zdskjcHourDataList: {
    rain: [],
    tem: []
  },
  gdskjcCurrentActive: 'none',
  // 所有数据
  lqAllDataList: [],
  gdskjcDataList: {},
  gdskjcCurrentTime: '',
  // 地图加载
  mapLoading: false
})

export const fireInsuranceStore = defineStore({
  id: 'forestry',
  state: () => {
    return defaultData()
  },
  getters: {
    // 站点实况监测 当前选中单位
    zdskjcCurrentSelectUnit: state =>
      state.zdskjcOptions?.find(i => i.value === state.zdskjcCurrentSelect)
        ?.unit || '',
    zdskjcCurrentSelectType: state =>
      state.zdskjcOptions?.find(i => i.value === state.zdskjcCurrentSelect)
        ?.type || '',
    // 当前时间数据
    gdskjcDataList: state => {
      return state.gdskjcAllDataList.find(
        i =>
          dayjs(i.reportTime).format('YYYY-MM-DD HH:mm:00') ===
          dayjs(state.gdskjcCurrentTime).format('YYYY-MM-DD HH:mm:00')
      )
    }
  },
  actions: {
    async handleGetStationInfo() {
      this.mapLoading = true
      this.mapLoading = false
    },
    handleGetGridPointMonitoringLegendData() {
      this.gdskjcOptions.forEach(async item => {
        if (item.legendStyle) {
          let legendStyle = item.legendStyle
          if (legendStyle.indexOf('-'))
            legendStyle = legendStyle.replace('-', '.')
          item.legendStops = await getJSON(
            `/public/demos/styles/${legendStyle}.color.rules.json`
          )
        }
      })
    },
    // 销毁 并初始化数据
    handleUnmountedInitData() {
      const defaultDataObj = defaultData()
      Object.keys(defaultDataObj).forEach(item => {
        this[item] = defaultDataObj[item]
      })
    }
  }
})
